package com.haoan.index.service.impl;


import java.util.List;
import javax.annotation.Resource;

import com.haoan.common.exception.ServiceException;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.haoan.index.common.enums.ValiFlag;

import com.haoan.index.mapper.VideoMapper;
import com.haoan.index.domain.Video;
import com.haoan.index.service.VideoService;

/**
 * 视频Service业务层处理
 *
 * @author autoGen
 * @date 2025-01-03
 */
@Service
public class VideoServiceImpl extends ServiceImpl<VideoMapper,Video> implements VideoService
{
    /**
     * 查询视频
     *
     * @param id 视频主键
     * @return 视频
     */
    @Override
    public Video selectVideoById(Long id)
    {
        return baseMapper.selectById(id);
    }

    /**
     * 查询视频列表
     *
     * @param video 视频
     * @return 视频
     */
    @Override
    public List<Video> selectVideoList(Video video)
    {
        LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
        //todo 添加查询条件
        queryWrapper.eq(video.getId() != null, Video::getId, video.getId());

        queryWrapper.eq(video.getChineseTime() != null, Video::getChineseTime, video.getChineseTime());
        queryWrapper.eq(video.getWriting() != null, Video::getWriting, video.getWriting());
        queryWrapper.eq(video.getVideo() != null, Video::getVideo, video.getVideo());
        queryWrapper.eq(video.getCreateTime() != null, Video::getCreateTime, video.getCreateTime());
        queryWrapper.eq(video.getCreateBy() != null, Video::getCreateBy, video.getCreateBy());
        queryWrapper.eq(video.getUpdateTime() != null, Video::getUpdateTime, video.getUpdateTime());
        queryWrapper.eq(video.getUpdateBy() != null, Video::getUpdateBy, video.getUpdateBy());
        queryWrapper.eq(Video::getValiFlag,ValiFlag.VALID );
        if ("1".equals(video.getType())){
            queryWrapper.ne(Video::getTitle,"春").ne(Video::getTitle,"夏").ne(Video::getTitle,"秋").ne(Video::getTitle,"冬");
        }else {
            queryWrapper.eq(video.getTitle() != null, Video::getTitle, video.getTitle());
        }
        return baseMapper.selectList(queryWrapper);
    }

    /**
     * 新增视频
     *
     * @param video 视频
     * @return 结果
     */
    @Transactional
    @Override
    public int insertVideo(Video video)
    {
        LambdaQueryWrapper<Video> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Video::getTitle,video.getTitle());
        queryWrapper.eq(Video::getValiFlag,ValiFlag.VALID );
        Video one = baseMapper.selectOne(queryWrapper);
        if (null != one){
            throw new ServiceException("该标题下已存在视频，只可修改！");
        }
        return baseMapper.insert(video);
    }

    /**
     * 修改视频
     *
     * @param video 视频
     * @return 结果
     */
    @Transactional
    @Override
    public int updateVideo(Video video)
    {
        return baseMapper.updateById(video);
    }

    /**
     * 批量删除视频
     *
     * @param ids 需要删除的视频主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteVideoByIds(Long [] ids)
    {
        Video video = new Video();
        video.setValiFlag(ValiFlag.INVALID);
        LambdaQueryWrapper<Video> updQw = new LambdaQueryWrapper<>();
        updQw.in(Video::getId, ids);
        return baseMapper.update(video, updQw);
    }

    /**
     * 删除视频信息
     *
     * @param id 视频主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteById(Long id)
    {
        Video video = getById(id);
        video.setValiFlag(ValiFlag.INVALID);
        return baseMapper.updateById(video);
    }
}
